import Image, ImageFont, ImageDraw, ConfigParser
from os.path import splitext, isfile, basename, dirname
def img_borders(image, bg="#000000", fg="#ffffff"):
    old_size = image.size
    paddingTop = 75
    paddingRight = 75
    paddingLeft = 75
    paddingBottom = 150
    borderSize = 2
    border = Image.new("RGB", (old_size[0]+borderSize*2, old_size[1]+borderSize*2), fg)
    img = Image.new("RGB", (old_size[0]+paddingRight+paddingLeft, old_size[1]+paddingTop+paddingBottom), bg)
    img.paste(border, (paddingLeft-borderSize, paddingTop-borderSize))
    img.paste(image, (paddingLeft, paddingTop))
    return img
def txt2img(image, text, fg="#ffffff", bg="#000000", font="verdana.ttf", FontSize=14, Position = (0,0), Align = 'LEFT'):
    fnt = ImageFont.truetype(font, FontSize)
    lineWidth = 20
    imgbg = Image.new('RGBA', image.size, bg) # make an entirely black image
    mask = Image.new('L',image.size,bg)       # make a mask that masks out all
    draw = ImageDraw.Draw(image)                     # setup to draw on the main image
    drawmask = ImageDraw.Draw(mask)                # setup to draw on the mask
    drawmask.line((0, lineWidth/2, image.size[0],lineWidth/2),
                  fill=bg, width=10)        # draw a line on the mask to allow some bg through
    image.paste(imgbg, mask=mask)                    # put the (somewhat) transparent bg on the main
    for line in text.split('\n'):
        if Align == 'CENTER':
            fontSize = fnt.getsize(line)
            LinePosition = ((image.size[0] - fontSize[0]) / 2, Position[1])
        else:
            if Align == 'RIGHT':
                fontSize = fnt.getsize(line)
                LinePosition = ((Position[0] - fontSize[0]), Position[1])
            else:
                LinePosition = Position
        draw.text(LinePosition, line, font=fnt, fill=fg)      # add some text to the main
        Position = (Position[0], Position[1] + FontSize + 5)
    del draw
    return image
class Card(object):
    def __init__(self, filePath = ''):
        self.categories = []
        self.description = ''
        self.type = ''
        self.name = ''
        self.tools = []
        self.decks = []
        self.author = ''
        self.date = ''
        self.title = ''
        self.filePath = filePath
        self.__init_from_info__()
    def __init_from_info__(self):
        infoFileName = splitext(self.filePath)[0]+".info"
        self.type = basename(dirname(self.filePath))
        self.name = splitext(basename(self.filePath))[0]
        if isfile(infoFileName):
            config = ConfigParser.ConfigParser()
            config.readfp(open(infoFileName))
            self.title = config.get('DATA', 'title')
            self.description = config.get('DATA', 'description')
            self.author = config.get('INFO', 'author')[len('$Author:'):len('$')*-1].strip()
            self.date = config.get('INFO', 'author')[len('Date:'):len('$')*-1].strip()
            self.categories = [item.strip() for item in config.get('DATA', 'categories').split(',')]
            self.tools = [item.strip() for item in config.get('DATA', 'tools').split(',')]
            self.decks = [item.strip() for item in config.get('DATA', 'decks').split(',')]
    def getImage(self):
        image = Image.open(self.filePath)
        size = image.size
        if size[0] > size[1]:
            size = (480,360)
        else:
            size = (360,480)
        image.thumbnail(size, Image.ANTIALIAS)
        image = img_borders(image)
        image = txt2img(image, self.title.upper(), font="verdanab.ttf",FontSize = 36, Align = 'CENTER', Position = (0, 10))
        image = txt2img(image, self.type.upper(), FontSize = 36, Align = 'CENTER', Position = (0, image.size[1]-150))
        image = txt2img(image, self.description, FontSize = 20, Align = 'CENTER', Position = (0, image.size[1]-110))
        image = txt2img(image, self.author, font="verdanab.ttf", Align = 'RIGHT', FontSize = 12, Position = (image.size[0]-75, 55))
        return image
    def __str__(self):
        return "Name: %s, Type: %s, Categories: %s, Filepath: %s" % (self.name, self.type, self.categories, self.filePath)